package org.dreamwork.jasmine2.events;

import org.dreamwork.jasmine2.engine.IValidatable;
import org.dreamwork.jasmine2.web.IWebControl;

import java.util.List;
import java.util.ArrayList;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2009-7-23
 * Time: 11:58:53
 */
public class ValidatableDelegate implements IValidatable {
    private final Vector<IValidateListener> validateListeners = new Vector<IValidateListener> ();
    
    public synchronized void addValidateListener (IValidateListener listener) {
        validateListeners.add (listener);
    }

    public synchronized void removeValidateListener (IValidateListener listener) {
        validateListeners.removeElement (listener);
    }

    public IValidateListener[] getValidateListners () {
        List<IValidateListener> list = new ArrayList<IValidateListener> ();
        synchronized (validateListeners) {
            list.addAll (validateListeners);
        }
        IValidateListener[] array = new IValidateListener[list.size ()];
        return list.toArray (array);
    }

    public boolean fireValidateListeners (IWebControl sender) {
        IValidateListener[] array = getValidateListners ();
        for (IValidateListener anArray : array) {
            if (!anArray.onValidate (sender))
                return false;
        }
        return true;
    }
}
